/*=======================================================================================
	Dynamics_monthly_nonparametric.do
	
		Uses the Personal Finance Website (PFW) data to analyze the response of 
		consumption to the APFD payments
		
	Author: Lorenz Kueng, Sep 2017
=========================================================================================*/

cap log close PFW_04a
log using "$homedir/log-files/PFW_04a_$date.log", text replace name(PFW_04a)
	
	
*=============================================================
* Nonparametric Analysis
*=============================================================

use "$homedir/data/stata/PFW_monthly.dta", clear
	
	generate date = Month


	*** Number of days per month
	
		cap drop days
		generate days =.
		replace  days = 31 if month==1
		replace  days = 28 if month==2 & year!=2012
		replace  days = 29 if month==2 & year==2012
		replace  days = 31 if month==3
		replace  days = 30 if month==4
		replace  days = 31 if month==5
		replace  days = 30 if month==6
		replace  days = 31 if month==7
		replace  days = 31 if month==8
		replace  days = 30 if month==9
		replace  days = 31 if month==10
		replace  days = 30 if month==11
		replace  days = 31 if month==12

		cap label drop month
		label define month	1 "jan" ///
							2 "feb" ///
							3 "mar" ///
							4 "apr" ///
							5 "may" ///
							6 "jun" ///
							7 "jul" ///
							8 "aug" ///
							9 "sep" ///
						   10 "oct" ///
						   11 "nov" ///
						   12 "dec" 
		label value month month


	*** Sample selection
	
		keep if APFDid_annual!=. 
		
		summarize family_size family_sizeImp family equivalence, detail

	
	*** Per capita spending and income
		
		** Create variables
		
			** Spending (nondurables and services)
			
				generate nds = - nondur_serv 
				replace  nds = nds * 30/days // 30-day equivalence month

				generate ndsPerCap = nds/family_sizeImp // per capita
				generate ndsOECDeq = nds/equivalence // using OECD equivalence scale (as a robustness check)
				
			** Income	

				generate inc = income * 30/days // 30-day equivalence month
		
				generate incPerCap = inc/family_sizeImp // per capita
				generate incOECDeq = inc/equivalence // using OECD equivalence scale
		
		
		** Winsorize and create variables for collapsing
		
			** Spending
			
			foreach var in PerCap OECDeq { 
			
				display _n(5)"nds`var'"_n(2)
				
				** Winsorize
				
				cap drop _temp
				winsor nds`var', generate(_temp) p(0.01)	
				sum nds`var' _temp
				bysort Alaska: summarize nds`var' _temp, detail	
				replace nds`var' = _temp
				drop _temp
			
				** Calculate within-household changes (levels and logs)
				
				xtset userid Month

				generate D_nds`var'_mean = D.nds`var'
				generate D_nds`var'_sd   = D.nds`var'
				generate D_nds`var'_med  = D.nds`var'

				generate  nds`var'_mean = nds`var'
				generate  nds`var'_sd   = nds`var'
			}
			
			** Income

			foreach var in PerCap OECDeq { 
			
				display _n(5)"inc`var'"_n(2)
				
				** Winsorize
				
				cap drop _temp
				winsor inc`var', generate(_temp) p(0.01)	
				sum inc`var' _temp
				bysort Alaska: summarize inc`var' _temp, detail	
				replace inc`var' = _temp
				drop _temp
			
				** Calculate within-household changes (levels and logs)
				
				xtset userid Month

				generate D_inc`var'_mean = D.inc`var'
				generate D_inc`var'_sd   = D.inc`var'
				generate D_inc`var'_med  = D.inc`var'

				generate  inc`var'_mean = inc`var'
				generate  inc`var'_sd   = inc`var'
			}
				
	generate N = 1
	
	save "PFW_04a_temp.dta", replace
	
	
	
	
	*-------------------------------
	* Collapse by month
	*-------------------------------

	use "PFW_04a_temp.dta", clear
	
		collapse (mean)   *_mean /// 
				 (median) *_med /// 
				 (sd)     *_sd ///
				 (sum)    N ///
			, by(Alaska month)

		** Calculate standard errors
		
		foreach var in PerCap OECDeq { 
		
			** Spending
			generate SE_D_nds`var' = D_nds`var'_sd/sqrt(N)
			
			generate SE_nds`var'   = nds`var'_sd/sqrt(N)
			
			generate SE_D_nds`var'_up     = D_nds`var'_mean + 2*SE_D_nds`var'
			generate SE_D_nds`var'_down   = D_nds`var'_mean - 2*SE_D_nds`var'
		
			generate SE_nds`var'_up       = nds`var'_mean   + 2*SE_nds`var'
			generate SE_nds`var'_down     = nds`var'_mean   - 2*SE_nds`var'
			
			** Income
			generate SE_D_inc`var'    = D_inc`var'_sd/sqrt(N)
		}
		
		** Reshape to wide
		
		rename Alaska A
		reshape wide *nds* N *inc*, j(A) i(month) 


		
		*------------------------------
		* Alaska-Washington difference
		*------------------------------
	
		foreach var in PerCap { // PerCap OECDeq { 
	
			tsset month
			
			*** spending changes
			
			cap drop diff*
			generate diff      = D_nds`var'_mean1 - D_nds`var'_mean0
			generate diff_SE   = sqrt( SE_D_nds`var'1^2 + SE_D_nds`var'0^2 )
			generate diff_up   = diff + 2*diff_SE
			generate diff_down = diff - 2*diff_SE

			cap drop mlab 
			generate mlab = string(diff,"%3.0f")

			twoway connected diff      month, lw(medium) lp(solid) msymbol(Oh) mcolor(black)  lc(black) mlabel(mlab) mlabc(black) mlabposition(9) || ///
				   line      diff_up   month, lw(thin)   lp(dash) lc(black)  || ///
				   line      diff_down month, lw(thin)   lp(dash) lc(black)  || ///
				, graphregion(color(white) lstyle(none)) ///
				  xlabel(1(1)12, valuelabel) xtitle("") ///
				  ylabel(, angle(90)) ytitle("difference in monthly per capita spending changes") ///
				  legend(off) yline(0, lc(black) lp(solid) lw(thin)) xline(10, lp(shortdash) lw(medium) lc(blue))

				local file "$homedir/results/figures/PFW_D_nds`var'_mean_monthly_diff_with_labels"
					graph export "`file'.eps", replace
					cap rm       "`file'.pdf"
					!epstopdf    "`file'.eps"
					rm           "`file'.eps"
					graph export "`file'.tif", replace

				
			display _n(2)"D_nds`var'_mean"_n(1)
			cap drop t_statistic 
			generate t_statistic = diff/diff_SE
			list month t_statistic diff, separator(0)

			
			cap drop diff*
			generate diff      = D_nds`var'_med1 - D_nds`var'_med0
			generate diff_SE   = sqrt(_pi/2) * sqrt( SE_D_nds`var'1^2 + SE_D_nds`var'0^2 )
			generate diff_up   = diff + 2*diff_SE
			generate diff_down = diff - 2*diff_SE
						
			cap drop mlab 
			generate mlab = string(diff,"%3.0f") 
			
			twoway connected diff      month, lw(medium) lp(solid) msymbol(Oh) mcolor(black)  lc(black) mlabel(mlab) mlabc(black) mlabposition(9) || ///
				   line      diff_up   month, lw(thin)   lp(dash) lc(black)  || ///
				   line      diff_down month, lw(thin)   lp(dash) lc(black)  || ///
				, graphregion(color(white) lstyle(none)) ///
				  xlabel(1(1)12, valuelabel) xtitle("") ///
				  ylabel(, angle(90)) ytitle("difference in monthly per capita spending changes") ///
				  legend(off) yline(0, lc(black) lp(solid) lw(thin)) xline(10, lp(shortdash) lw(medium) lc(blue))

				local file "$homedir/results/figures/PFW_D_nds`var'_median_monthly_diff_with_labels"
					graph export "`file'.eps", replace
					cap rm       "`file'.pdf"
					!epstopdf    "`file'.eps"
					rm           "`file'.eps"
					graph export "`file'.tif", replace
			
			display _n(2)"D_nds`var'_median"_n(1)
			cap drop t_statistic 
			generate t_statistic = diff/diff_SE
			list month t_statistic diff, separator(0)
		

			*** income changes
			
			cap drop diff*
			generate diff      = D_inc`var'_med1 - D_inc`var'_med0
			generate diff_SE   = sqrt(_pi/2) * sqrt( SE_D_inc`var'1^2 + SE_D_inc`var'0^2 )
			generate diff_up   = diff + 2*diff_SE
			generate diff_down = diff - 2*diff_SE
						
			cap drop mlab 
			generate mlab = string(diff,"%3.0f") // %3.2f

			twoway connected diff      month, lw(medium) lp(solid) msymbol(Oh) mcolor(black)  lc(black) mlabel(mlab) mlabc(black) mlabposition(9) || ///
				   line      diff_up   month, lw(thin)   lp(dash) lc(black)  || ///
				   line      diff_down month, lw(thin)   lp(dash) lc(black)  || ///
				, graphregion(color(white) lstyle(none)) ///
				  xlabel(1(1)12, valuelabel) xtitle("") ///
				  ylabel(, angle(90)) ytitle("difference in monthly per capita income changes") ///
				  legend(off) yline(0, lc(black) lp(solid) lw(thin)) xline(10, lp(shortdash) lw(medium) lc(blue))

				local file "$homedir/results/figures/PFW_D_inc`var'_median_monthly_diff_with_labels"
					graph export "`file'.eps", replace
					cap rm       "`file'.pdf"
					!epstopdf    "`file'.eps"
					rm           "`file'.eps"
					graph export "`file'.tif", replace
		
			display _n(2)"D_inc`var'_median"_n(1)
			cap drop t_statistic 
			generate t_statistic = diff/diff_SE
			list month t_statistic diff, separator(0)

					
	} // end loop over PerCap OECDeq

	
	
	
	*-------------------------------
	* Collapse by month and income quintile (for appendix)
	*-------------------------------

	use "PFW_04a_temp.dta", clear
	
	** Create income quintiles
	
		** income per household-equivalent
		generate incomeYeq = incomeY / equivalence
		lab var  incomeYeq  "annual after-tax income per household equivalence"
		
		** winsor
		winsor incomeYeq, p(0.01) generate(_temp)
		replace incomeYeq = _temp
		drop _temp
		
		** annualize
		bysort userid: egen _temp = mean(incomeYeq)
		replace incomeYeq = _temp
		drop _temp
	
		** compute quintiles 	
		egen incomeY_quintile = xtile(incomeYeq), n(5) by(year Alaska)	
	
	
	forvalues inc = 1(1)5 {
	
	** Collapse data
	preserve
		
		collapse (mean)   *_mean /// 
				 (median) *_med /// 
				 (sd)     *_sd ///
				 (sum)    N ///
			if incomeY_quintile==`inc' ///
			, by(Alaska month incomeY_quintile)

		** Calculate standard errors
		
		foreach var in PerCap { // PerCap OECDeq { 
		
			** Spending
			generate SE_D_nds`var' = D_nds`var'_sd/sqrt(N)
			
			generate SE_nds`var'   = nds`var'_sd/sqrt(N)
			
			generate SE_D_nds`var'_up     = D_nds`var'_mean + 2*SE_D_nds`var'
			generate SE_D_nds`var'_down   = D_nds`var'_mean - 2*SE_D_nds`var'
		
			generate SE_nds`var'_up       = nds`var'_mean   + 2*SE_nds`var'
			generate SE_nds`var'_down     = nds`var'_mean   - 2*SE_nds`var'
		
			** Income
			generate SE_D_inc`var'    = D_inc`var'_sd/sqrt(N)
		}
		
		** Reshape to wide
		
		rename Alaska A
		reshape wide *nds* N *inc*, j(A) i(month) 

	
		*------------------------------
		* Alaska-Washington difference
		*------------------------------
	
		foreach var in PerCap { // PerCap OECDeq { 
	
			tsset month


			*** income changes
			
			cap drop diff*
			generate diff      = D_inc`var'_med1 - D_inc`var'_med0
			generate diff_SE   = sqrt(_pi/2) * sqrt( SE_D_inc`var'1^2 + SE_D_inc`var'0^2 )
			generate diff_up   = diff + 2*diff_SE
			generate diff_down = diff - 2*diff_SE
						
			cap drop mlab 
			generate mlab = string(diff,"%3.0f") // %3.2f

			twoway connected diff      month, lw(medium) lp(solid) msymbol(Oh) mcolor(black)  lc(black) mlabel(mlab) mlabc(black) mlabposition(9) || ///
				   line      diff_up   month, lw(thin)   lp(dash) lc(black)  || ///
				   line      diff_down month, lw(thin)   lp(dash) lc(black)  || ///
				, graphregion(color(white) lstyle(none)) ///
				  xlabel(1(1)12, valuelabel) xtitle("") ///
				  ylabel(, angle(90)) ytitle("difference in monthly per capita income changes") ///
				  legend(off) yline(0, lc(black) lp(solid) lw(thin)) xline(10, lp(shortdash) lw(medium) lc(blue))

				local file "$homedir/results/figures/PFW_D_inc`var'_median_monthly_diff_IncomeQuintile`inc'"
					graph export "`file'.eps", replace
					cap rm       "`file'.pdf"
					!epstopdf    "`file'.eps"
					rm           "`file'.eps"
					graph export "`file'.tif", replace
		
			display _n(2)"D_inc`var'_median"_n(1)
			cap drop t_statistic 
			generate t_statistic = diff/diff_SE
			list month t_statistic diff, separator(0)
	
		} // end loop over PerCap OECDeq

	restore
	} // end loop over income quintiles
	
	
cap n rm "PFW_04a_temp.dta"

log close PFW_04a
